package io.gitee.zhangbinhub.admin.oauth.init

import io.gitee.zhangbinhub.acp.boot.base.BaseInitialization
import io.gitee.zhangbinhub.acp.boot.interfaces.LogAdapter
import io.gitee.zhangbinhub.admin.common.constant.RoleCode
import io.gitee.zhangbinhub.admin.oauth.domain.ApplicationDomain
import io.gitee.zhangbinhub.admin.oauth.domain.InitDataDomain
import io.gitee.zhangbinhub.admin.oauth.domain.RoleDomain
import io.gitee.zhangbinhub.admin.oauth.domain.UserDomain
import org.springframework.stereotype.Component

@Component
class DataInitialization(
    private val logAdapter: LogAdapter,
    private val applicationDomain: ApplicationDomain,
    private val userDomain: UserDomain,
    private val roleDomain: RoleDomain,
    private val initDataDomain: InitDataDomain
) : BaseInitialization() {
    override val name: String
        get() = "Data Initialization"

    override val order: Int
        get() = 1

    override fun start() {
        applicationDomain.getAppList().apply {
            if (this.isEmpty()) {
                logAdapter.info(">>>>>>>>>>>>>>>>>>>> begin init system resource data...")
                initDataDomain.doInitResources()
                logAdapter.info(">>>>>>>>>>>>>>>>>>>> init system resource data has finished!")
            }
        }
        userDomain.getUserVoListByRole(listOf(RoleCode.SUPER)).apply {
            if (this.isEmpty()) {
                logAdapter.info("admin user is not existed")
                logAdapter.info("begin init admin user...")
                roleDomain.getRoleList().filter { role -> role.code == RoleCode.SUPER }.let { roleList ->
                    initDataDomain.initAdminUser(roleList)
                }
                logAdapter.info("init admin user has finished!")
            }
        }
    }

    override fun stop() {}
}